import SignList from './components/SignList.vue'
import Upload2 from './components/Upload.vue'
import CouponList from './components/CouponList.vue'
import HxList from './components/HxList.vue'
import PlaceGoodsList from './components/placeGoodsList.vue'
export default () => {
  /**处理自定义组件 */
  const formatCustom = (renderOption, isRequest = false) => {
    console.log('🚀 ~ formatCustom ~ renderOption:', renderOption)
    Object.values(renderOption).forEach((val: any) => {
      if (val.type == 'custom') {
        if (isRequest) {
          if (val.component == 'SignList') val.component = SignList
          if (val.component == 'Upload2') val.component = Upload2
          if (val.component == 'CouponList') val.component = CouponList
          if (val.component == 'HxList') val.component = HxList
          if (val.component == 'PlaceGoodsList') val.component = PlaceGoodsList
        } else {
          val.component = String(val.component.__name)
        }
      }
    })
    return renderOption
  }

  /**
   * 数据库数据转换
   *  =_|转换成=>
   */
  const formatResquestRenderList = list => {
    list.map(_ => {
      Object.keys(_).forEach(key => {
        const value = _[key]
        if (typeof value == 'string' && value.indexOf('_|') != -1) {
          /** 解析特殊符号 */
          _[key] = eval(value.replace('_|', '>'))
        }
      })
    })
    return list
  }
  /**
   * 箭头函数转换数据存数据库
   * => 转换成 =_|
   */
  const formatRenderList = list => {
    return JSON.stringify(list, (_, val) => {
      if (typeof val === 'function') {
        /** 特殊符号>存不进数据库转换一次 */
        return val.toString().replace('>', '_|')
      }
      return val
    })
  }
  return { formatCustom, formatResquestRenderList, formatRenderList }
}
